package com.example.edu_service.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.edu_service.entity.EduSubject;
import com.example.edu_service.entity.excel.SubjectData;
import com.example.edu_service.entity.subject.One;
import com.example.edu_service.entity.subject.Two;
import com.example.edu_service.listener.SubjectExcelListener;
import com.example.edu_service.mapper.EduSubjectMapper;
import com.example.edu_service.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-31
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {

        try {
            InputStream in= file.getInputStream();

            EasyExcel.read(in,SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();

        }

    }

    @Override
    public List<One> getAllSubject() {

        QueryWrapper<EduSubject> wrapperone= new QueryWrapper<>();
        wrapperone.eq("parent_id","0");

        List<EduSubject> onelist = baseMapper.selectList(wrapperone);

        QueryWrapper<EduSubject> wrappertwo= new QueryWrapper<>();
        wrapperone.ne("parent_id","0");

        List<EduSubject> twolist = baseMapper.selectList(wrappertwo);

        List<One> mainlist = new ArrayList<>();

        for (int i = 0; i < onelist.size(); i++) {

            EduSubject eduSubject = onelist.get(i);
            One one=new One();
            BeanUtils.copyProperties(eduSubject,one);
            mainlist.add(one);

            List<Two> twomainlist = new ArrayList<>();

            for (int m = 0; m < twolist.size(); m++) {
                EduSubject subject = twolist.get(m);

                if (subject.getParentId().equals(eduSubject.getId())){
                    Two two = new Two();
                    BeanUtils.copyProperties(subject,two);
                    twomainlist.add(two);
                }

            }
            one.setChildren(twomainlist);

        }


        return mainlist;
    }
}
